package com.yand.controller.business.custom;


import com.github.pagehelper.PageInfo;
import com.yand.api.entity.business.custom.THfStudent;
import com.yand.api.entity.common.Result;
import com.yand.api.service.business.custom.StudentService;
import com.yand.api.service.business.order.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 *  学生相关controller
 */
@Api(tags = "学生控制器类")
@RestController
@RequestMapping("student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private OrderService service;

    @ApiOperation("跳转学生详情页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "student", value = "学生明细", required = true)
    })
    @GetMapping("/list")
    public ModelAndView list(THfStudent student, Model model){
        ModelAndView modelAndView = new  ModelAndView();

        modelAndView.setViewName("business/custom/student-list");
        PageInfo<THfStudent> studentPageInfo = studentService.queryAllStudentList(student);
        model.addAttribute("pageInfo",studentPageInfo);

        model.addAttribute("student",student);

        return modelAndView;
    }

    @GetMapping("/user/{studentId}")
    public ModelAndView toEditUserPage(@PathVariable(value = "studentId") Long studentId, Model model){
        ModelAndView modelAndView = new ModelAndView();
        String typeSuffix = (studentId == 0L) ? "add":"edit";
        modelAndView.setViewName("business/custom/student-"+typeSuffix);
        THfStudent student = studentService.getStudentBean(studentId);
        model.addAttribute("student",student);
        return modelAndView;
    }


    /**
     * 添加学生信息
     * @param student
     * @return
     */
    @ApiOperation("添加学生信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user", value = "student对象", required = true)
    })
    @PostMapping("/user")
    public Result addStudent(THfStudent student){

        studentService.addStudent(student);
        return new Result(0);
    }


    @ApiOperation("学生流转-转交")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user", value = "student对象", required = true)
    })
    @PostMapping("/apply/float")
    public Result applyFinance(String studentId, String status){

        studentService.updateStudentStatus(studentId, status);

        return new Result(0);
    }

    @ApiOperation("财务审核")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user", value = "student对象", required = true)
    })
    @PostMapping("/audit/finance")
    public Result auditFinance(String studentId, String status){
        studentService.updateStudentStatus(studentId,status);
        service.updateOrderStatus(studentId, status);
        return new Result(0);
    }


    @ApiOperation("学管分配")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user", value = "student对象", required = true)
    })
    @PostMapping("/divide/teacher")
    public Result divideTeacher(String studentId,String teacherId, String status){
        THfStudent student = new THfStudent();
        student.setId(Integer.parseInt(studentId));
        student.setBelongteacher(Integer.parseInt(teacherId));
        student.setExtand1(status);
        studentService.updateStudent(student);
        return new Result(0);
    }








}
